package com.example.rentalcar;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;

import java.io.ByteArrayInputStream;

public class OssConnectionTester {

    public static void main(String[] args) {
        // 从OSS获取的endpoint、accessKeyId、accessKeySecret等参数
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        String accessKeyId = "LTAI5tCPPxidipJatuWWpPWr";
        String accessKeySecret = "YK1P9sfOJPOwwnx7U34lwTzlQCjtNl";
        String bucketName = "zllbucket1";
        String objectName = "card/";

        // 创建OSSClient
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 尝试上传一个空文件，如果成功上传则说明连接正常
            byte[] content = new byte[0];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(0);
            ossClient.putObject(bucketName, objectName, inputStream, metadata);

            System.out.println("OSS连接成功！");
        } catch (Exception e) {
            // 连接失败，打印异常信息
            e.printStackTrace();
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }
}
